//
//  ZBDTabBarController.m
//  ZDB
//

//

#import "ZBDTabBarController.h"
#import "ZBDBaseNavigationViewController.h"

@interface ZBDTabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@end

@implementation ZBDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //去黑线
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [UITabBar appearance].layer.borderWidth = 0.0f;
    [UITabBar appearance].clipsToBounds = YES;
    self.tabBar.barTintColor = WHITECOLOR;
    [[UITabBar appearance] setTranslucent:NO];
    
    self.delegate =self;
    
    NSMutableArray *viewCtls = [NSMutableArray arrayWithCapacity:kMTBaseTabbarCount];
    for (int i=0; i<kMTBaseTabbarCount; i++) {
        
        ZBDBaseNavigationViewController *navCtl=[[ZBDBaseNavigationViewController alloc]initWithRootViewController:kMTBaseTabbarStoryboardNames[i]];
        UIViewController *viewCtl = navCtl.topViewController;
        viewCtl.tabBarItem.title = kMTBaseTabbarTitles[i];
        UIImage *imagen = [UIImage imageNamed:kMTBaseTabbarNormalImages[i]];
        imagen=[imagen imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //不让图片渲染
        viewCtl.tabBarItem.image = imagen;
        viewCtl.tabBarItem.tag = i;
        
        UIImage *image=[UIImage imageNamed:kMTBaseTabbarSelectImages[i]];
        image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //不让图片渲染
        viewCtl.tabBarItem.selectedImage=image;
        [viewCtl.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2)];
//        [viewCtl.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 4)];
        // 设置 tabbarItem 选中状态下的文字颜色(不被系统默认渲染,显示文字自定义颜色)
        NSDictionary *dictHome = [NSDictionary dictionaryWithObject:MAINTITLECOLOR1 forKey:NSForegroundColorAttributeName];
        NSDictionary *dictHome1 = [NSDictionary dictionaryWithObject:TABTITLECOLOR forKey:NSForegroundColorAttributeName];
        [navCtl.tabBarItem setTitleTextAttributes:dictHome forState:UIControlStateNormal];
        [navCtl.tabBarItem setTitleTextAttributes:dictHome1 forState:UIControlStateSelected];
        [viewCtls addObject:navCtl];
        
        
    }
    self.viewControllers = viewCtls;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
    
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    pulse.duration = 0.2;   //time
    pulse.repeatCount = 1;  //重复次数
    pulse.autoreverses = YES;   //回到初始状态
    pulse.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
    pulse.toValue = [NSNumber numberWithFloat:1.4];     //结束伸缩倍数
    UIImageView * tabbarSwappableImage = [self getBarButtonviewWithCurrentVC:viewController];
    [tabbarSwappableImage.layer addAnimation:pulse forKey:@"animateTransform"];
    return YES;
}
- (UIImageView *)getBarButtonviewWithCurrentVC:(UIViewController *)currentViewController
{
    UIControl * tabbarButton = [currentViewController.tabBarItem valueForKey:@"view"];
    
    for (UIImageView * tabbatSwappableImage in tabbarButton.subviews)
    {
        return tabbatSwappableImage;
    }
    return nil;
}

@end
